/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import httpurl from "../../../unit/common/js/httpurl";
import {qotCntTyp, matUnt} from "../config";
import bidInfoTpl from "./bid_info.handlebars";
import bidProdTpl from "./bid_prod.handlebars";


jQuery(function () {
    App.init();

    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };

    var projId = httpurl.uri.params.id;
    var bidId = httpurl.uri.params.bidId;
    let token = sessionStorage.getItem('supplier_token');

    // 提交报价
    $('.J_submit').click(function(){
        let error = '';
        let servPrms = $('#servPrms')[0].value;
        let qotMemo = $('#qotMemo')[0].value;
        console.log(servPrms);

        let $matDtls = $('.J_prod-item');
        let matDtls = [];
        $matDtls.each((index, item)=>{
            let matDtl = {};
            matDtl.matCod = $(item).data('matcod')||'C000001';
            matDtl.matNam = $(item).find('.J_matNam').html();
            matDtl.matUnt = $(item).find('.J_matUnt').data('matunit')||'个';
            matDtl.price = $(item).find('.J_price')[0].value;
            matDtl.splNum = $(item).find('.J_splNum')[0].value;
            if(!matDtl.price || !matDtl.splNum){
                error += '产品' + (index+1) + '报价信息不可为空';
            }
            matDtls.push(matDtl);
        })
        if(error!==''){
            alert(error);
        }
        let data = {
            "bidId": bidId,
            "servPrms": servPrms,
            "qotMemo": qotMemo,
            "dtls": matDtls
        }

        $.ajax({
            url: HOST_ADDR + '/cpmp/bidProjOnMngForSplr/qot',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            access_token: token,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp){
                if(resp.success){
                    console.log(resp);
                    alert('报价成功');
                    location.href = '/supplier_self/my_bidding_list.html';
                }else{
                    alert(resp.msgInf);
                }
            },
            error:function(err){
                console.log(err);
            }
        });

    });

    jQuery.ajax({
//        url: 'http://47.92.5.0:8080/cpmp/bidProjOn/queryByConds/1/20',cpmp/bidProjOnMngForSplr/viewProjDtl/1
        url: HOST_ADDR+'/cpmp/bidProjOnMngForSplr/viewProjDtl/'+projId,
        type: 'get',
        // data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
//         access_token: token,
        headers: {
            'access_token': token,
            'login_type': 'splr'
        },
        success: function(resp){
            if (!resp.success) {
                console.log(resp.msgCod+' '+resp.msgInf);
                return;
            }
            resp.data.bidProjOn.qotCntTyp = qotCntTyp[resp.data.bidProjOn.qotCntTyp] || '';
            resp.data.bidProjOn.bidRngTyp=resp.data.bidProjOn.bidRngTyp == 1?'定向招标':'非定向招标';

            if (resp.data.bidProjOn.bidOpenTim != null&&resp.data.bidProjOn.bidOpenTim !='') {
                resp.data.bidProjOn.bidOpenTim = (new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            } else {
                resp.data.bidProjOn.bidOpenTim = '';
            }
            if (resp.data.bidProjOn.bidEndTim != 'null'&&resp.data.bidProjOn.bidEndTim !='') {
                resp.data.bidProjOn.bidEndTim = (new Date(resp.data.bidProjOn.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            } else {
                resp.data.bidProjOn.bidEndTim= '';
            }
            $.each(resp.data.matDtls,function(index,item){
                item.matUntFmt = matUnt[item.matUnt];
                console.log(item.matUnt);
            });

            let infoHtml = bidInfoTpl(resp.data.bidProjOn);
            let prodHtml = bidProdTpl({matDtls: resp.data.matDtls});

            jQuery('.basicInfo').html(infoHtml);
            jQuery('.basicInfo').after(prodHtml);
            console.log(resp)


        },
        error:function(err){
            console.log(err);
        }
    });
});